import _ from 'lodash';
import Base from './core/Base.js';
import * as THREE from 'three';
import Renderer from './core/Renderer.js';
import Director from './core/Director.js';
import Artist from './core/Artist.js';

// window.THREE = THREE;

class Application extends Base {
    constructor() {
        super();
        this._name = 'Application';

        this.viewport = { el: null, width: 0, height: 0, ratio: 2, x: 0, y: 0, pointer: { x: 0, y: 0, z: 0 } };
        this.container = null;
        this.settings = {};

        this.defaultOffset = { x: 0, y: 0, z: 0 };
        this.defaultCamera = { x: 100, y: 800, z: 650 };

        this.scene = null;
        this.camera = null;
        this.renderer = null;
        this.labelRenderer = null;
        this.artist = null;
        this.director = null;
    }

    destory() {
        // this.director && (this.director.isRunning = false);
        // this.stop();
        console.warn('Application.destory: ');
        super.destory();
    }

    setStaticPath(path) {
        this.staticPath = path;
    }
    init(params) {
        this.settings = _.cloneDeep(params);
        let { container, offset, labelsHelper } = this.settings;
        console.log('Application.init: ', this);

        if (offset) this.setDefaultOffset(offset);
        this.viewport = Artist.caculateViewport(container);
        this.renderer = Renderer.getWebGLRenderer(this.viewport);
        if (labelsHelper) this.labelRenderer = Renderer.getCSS2DRenderer(this.viewport);
        // console.log('Application.init: ', labelsHelper, this.labelRenderer);

        this.artist = new Artist(this, this.viewport);
        this.artist.init(params);
        this.scene = this.artist.getCurrentScene();
        this.camera = this.artist.camera;
        this.director = new Director(this, this.artist);
    }

    cameraChanged(camera) {
        // this.camera.position.set(camera.x, camera.y, camera.z);
        this.artist.initCamera(camera);
    }
    topViewChanged(res) {
        if (this.artist) this.artist.topViewChanged(res);
    }
    controlsChanged(res) {
        let { enabled } = res;
        if (!this.artist.controls) this.artist.initControls();

        this.artist.controls.enabled = !enabled;
    }

    start() {
        if (this.director) this.director.start();
    }
    running() {
        if (this.labelRenderer) this.labelRenderer.render(this.scene, this.camera);
    }
    stop() {
        if (this.director) this.director.stop();
    }
    isRunning() {
        return this.director && this.director.isRunning;
    }

    resetCamera() {
        this.artist.resetCamera();
    }
    moveCamera({ x, y, z, lx, ly, lz } = {}) {
        this.artist.moveCamera({ x, y, z, lx, ly, lz });
    }

    setDefaultCamera({ x, y, z } = {}) {
        this.defaultCamera = { x, y, z };
    }

    setDefaultOffset({ x = 0, y = 0, z = 0 } = {}) {
        this.defaultOffset = { x, y, z };
    }

    cameraChanged(val) {
        this.artist.initCamera(val);
    }
    rotatableChanged(val) {
        let rotationSpeed = val ? this.settings.rotationSpeed || 0.0005 : 0;
        this.director.rotatableChanged(val, rotationSpeed);
    }
    enableRotate(val) {
        this.artist.enableRotate(val);
    }
    showLabelsChanged(val) {
        this.settings.showLabels = val;
    }
    showAlarmChanged(val) {
        this.settings.showAlarm = val;
    }
    lightChanged(val) {
        this.artist && this.artist.initLight(val);
    }
    skyChanged(val) {
        this.artist && this.artist.initSky(val);
    }
    floorChanged(val) {
        this.artist && this.artist.initFloor(val);
    }
    gridHelperChanged(val) {
        this.artist && this.artist.initGrid(val);
    }
    effectChanged(val) {
        this.artist && this.artist.initEffect(val);
    }

    // draw
    handlePainting(args) {
        this.director.handlePainting(args);
    }
}

export default Application;
